import "crvpuppet"

# Definition: at3_xrootd_node
#
#  Xrootd is a high performance network storage system widely used in high energy 
#  physics experiments such as ATLAS and ALICE. The underlying Xroot data transfer 
#  protocol provides highly efficient access to ROOT based data files.  
#  
#  This is the configuration for and ATLAS Tier 3 worker node it has a
#  data server.
#
# Parameters:
# 	- $webaddr: the web address to the cloud crv server
#   - $roleid: unique identifier of the role for this module
#
# CloudCRV Attributes:
#   - $fqdnRedirector : fully qualified domain name for the redirector
#   - $inventory: inventory directory for the Simple Service Inventory. 
#      Default value is'/atlas/inventory'    
#   - $ossLocalRoot: Local root for the data server inventories. Default is'/local/xrootd/a'
#   - $storagePath: a directory containing internal Xrootd references to files in the system.
#      Default is '/atlas'
#   
# Actions:
#   - installs xrootd-server
#   - sets up the storage directorys, prepares configuration files for xrootd and the
#     the simple storage inventory
#   - starts the xrootd and cmsd services
#
# Requires:
#   
# Author: Val Hendrix <vchendrix@lbl.gov>
#
define at3_xrootd_node ($webaddr="", $roleid="") 
{

  
  
  
  if $AutoMode==true and $CloudCRV==true {
    $fqdnRedirector=get_provider_attr($webaddr,$roleid,"XROOTDRedirector")
    $inventory=get_role_attr($webaddr,$roleid,"XROOTDInventory")
    $ossLocalRoot=get_role_attr($webaddr,$roleid,"XROOTDOssLocalRoot")
    $storagePath=get_role_attr($webaddr,$roleid,"XROOTDStoragePath")
     
  }
  else
  {
  	  $inventory='/atlas/inventory'
	  $ossLocalRoot='/local/xrootd/a'
	  $storagePath='/atlas'
  }
  
  package { ['xrootd-server.x86_64','xrootd-client.x86_64','xrootd-fuse.x86_64','xrootd-debuginfo.x86_64','xrootd-libs.x86_64']:
      ensure => installed,
  }
       
 

  # create the storage cache that will be different from /tmp.
  exec { "${ossLocalRoot}${storagePath}":
    command => "mkdir -p ${ossLocalRoot}${storagePath}",
    path => ['/bin','/usr/bin'],
    creates => "${ossLocalRoot}${storagePath}",
    require => [Package["xrootd-server.x86_64"],Package["xrootd-client.x86_64"]],
    logoutput=>true, 
  }
 
  exec { "chown-osslocalroot":
    command => "chown -R xrootd:xrootd ${ossLocalRoot}${storagePath}",
    path => ['/bin','/usr/bin'],
    require => Exec["${ossLocalRoot}${storagePath}"],
    logoutput=>true, 
    onlyif => ["test -z '`ls -l $ossLocalRootTop | grep xrootd`'"]
  }

  file { '/etc/xrootd/auth_file':
    ensure => present,
    owner  => 'xrootd',
    group  => 'xrootd',
    content => template('at3_xrootd_node/auth_file.tpl'),
    require => [Package["xrootd-server.x86_64"],Package["xrootd-client.x86_64"]],
  }
  
  file { "/etc/xrootd/xrootd-clustered.cfg":
    ensure  => present,
    content => template("at3_xrootd_node/xrootd-clustered.cfg.tpl"),
    require => [Package["xrootd-server.x86_64"],Package["xrootd-client.x86_64"]],
  }
    
  # start the xrootd service
  service { 'xrootd':
    name   => 'xrootd',
    ensure => 'running',
    enable => true,
    pattern => '/usr/bin/xrootd',
    require => [File["/etc/xrootd/xrootd-clustered.cfg"],File['/etc/xrootd/auth_file']],
   }

  # start the cluster management service
  service { 'cmsd':
    name   => 'cmsd',
    ensure => 'running',
    enable => true,
    require => [File["/etc/xrootd/xrootd-clustered.cfg"],File['/etc/xrootd/auth_file'],Exec["chown-osslocalroot"]],
   }
}


define at3_xrootd_node_reverse($webaddr, $roleid) {
  #set_role_attr($webaddr,$roleid,"ReverseProfileApplied","True")
}